#include "OfficeDocment.h"
#include "EleFactory.h"
#include "QFile"
#include "QFileInfo"
#include "QDir"
#include <QTextDocument>
#include <QPrinter>
#include <QDebug>
OfficeDocment::OfficeDocment(QObject *parent, QFile* fileName)
{
    if(fileName)
    {
        m_pFile =  fileName;
        m_pfileInfo = new QFileInfo(*m_pFile);
        m_sFileName =m_pfileInfo->baseName();
    }

}

OfficeDocment::OfficeDocment(QString fileName, QObject *parent)
{
    m_pFile = new QFile(fileName);
    m_pfileInfo = new QFileInfo(*m_pFile);
    m_sFileName = m_pfileInfo->baseName();
    qDebug()<<m_sFileName;
}

OfficeDocment::~OfficeDocment()
{
    if(m_pFile->isOpen()){
        m_pFile->close();
    }
    delete m_pFile;
    delete m_pfileInfo;
}

HtmlElement *OfficeDocment::creactEle(EleFactory::EleType type)
{
    return  this->eleFactory.eleTypeFactory(type);
}

bool OfficeDocment::printToFile(OfficeDocment::DomType type)
{
   if(!m_pFile->isOpen()){
        m_pFile->open(QIODevice::WriteOnly);
   }
    QString html ="<!DOCTYPE html>\n <html lang=\"zh\" xmlns:v=\"urn:schemas-microsoft-com:vml\"\
            xmlns:o=\"urn:schemas-microsoft-com:office:office\"\
            xmlns:w=\"urn:schemas-microsoft-com:office:word\"\
            xmlns:x=\"urn:schemas-microsoft-com:office:excel\"\
            xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\"\
            xmlns=\"http://www.w3.org/TR/REC-html40\">\
            <head>\
            <!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>工作表标题</x:Name><x:WorksheetOptions><x:Print><x:ValidPrinterInfo /></x:Print></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->\
            <!--[if gte mso 9]><xml><w:WordDocument><w:View>Print</w:View><w:TrackMoves>false</w:TrackMoves><w:TrackFormatting/><w:ValidateAgainstSchemas/><w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid><w:IgnoreMixedContent>false</w:IgnoreMixedContent><w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText><w:DoNotPromoteQF/><w:LidThemeOther>EN-US</w:LidThemeOther><w:LidThemeAsian>ZH-CN</w:LidThemeAsian><w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript><w:Compatibility><w:BreakWrappedTables/><w:SnapToGridInCell/><w:WrapTextWithPunct/><w:UseAsianBreakRules/><w:DontGrowAutofit/><w:SplitPgBreakAndParaMark/><w:DontVertAlignCellWithSp/><w:DontBreakConstrainedForcedTables/><w:DontVertAlignInTxbx/><w:Word11KerningPairs/><w:CachedColBalance/><w:UseFELayout/></w:Compatibility><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><m:mathPr><m:mathFont m:val=\"Cambria Math\"/><m:brkBin m:val=\"before\"/><m:brkBinSub m:val=\"--\"/><m:smallFrac m:val=\"off\"/><m:dispDef/><m:lMargin m:val=\"0\"/> <m:rMargin m:val=\"0\"/><m:defJc m:val=\"centerGroup\"/><m:wrapIndent m:val=\"1440\"/><m:intLim m:val=\"subSup\"/><m:naryLim m:val=\"undOvr\"/></m:mathPr></w:WordDocument></xml><![endif]-->\
            <title> One Test</title>\
            <meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\
            <meta name=ProgId content=Word.Document>\
            <meta name=Generator content=\"Microsoft Word 15\">\
            <meta name=Originator content=\"Microsoft Word 15\">\
            <link rel=File-List href=\"file3086.files/filelist.xml\">\
            <link rel=Edit-Time-Data href=\"file3086.files/editdata.mso\">\
            <link rel=themeData href=\"file3086.files/themedata.thmx\">\
            <link rel=colorSchemeMapping href=\"file3086.files/colorschememapping.xml\">\
            <style type=\"text/css\">\
                img{\
                width: 100px;\
                height: 100px;\
                display: block;\
                margin: 0px;\
            }\
            </style></head><body>";
    for(int i =0 ;i<eleLsit.size();i++)
    {

        html.append(eleLsit[i]->toString());
    }


    html.append("</body></html>");
    QString fileName;
    QFile file;
    fileName.clear();
    switch (type) {
    case doc:
    {
        fileName =m_pfileInfo->absolutePath()+"/"+m_sFileName+".doc";
        file.setFileName(fileName);
        file.open(QIODevice::WriteOnly);
        file.write(html.toUtf8());
        file.close();
        break;
    }
    case xls:
    {
        fileName =m_pfileInfo->absolutePath()+"/"+m_sFileName+".xls";
        file.setFileName(fileName);
        file.open(QIODevice::WriteOnly);
        file.write(html.toUtf8());
        file.close();
        break;
    }
    case pdf:
    {

        fileName =m_pfileInfo->absolutePath()+"/"+m_sFileName+".pdf";
        QPrinter pdf;
        QTextDocument pdf_txt;
        pdf.setPageSize(QPrinter::A4);
        pdf.setOutputFormat(QPrinter::PdfFormat);
        pdf.setOutputFileName(fileName);
        pdf_txt.setHtml(html);
        pdf_txt.print(&pdf);
        break;
    }
    default:
        break;
    }
    m_pFile->write(html.toUtf8());
    m_pFile->close();

}

void OfficeDocment::appendElement(HtmlElement *ele)
{
    eleLsit.append(ele);
}

QFile *OfficeDocment::getFilePath() const
{

    return m_pFile;
}

void OfficeDocment::setFilePath(QFile *value)
{
    m_pFile = value;
}
